﻿var keyVaule = request('keyVaule');
var openForm = request('openForm');
var bootstrap = function ($, learun) {
    "use strict";

    $.lrSetForm(top.$.rootUrl + '/LR_SystemModule/Annexes/GetAnnexesFileList?folderId=' + keyVaule, function (data) {
        for (var i = 0, l = data.length; i < l; i++) {
            $('#lr_form_file_queue .lr-form-file-queue-bg').hide();
            var item = data[i];
            var $item = $('<div class="lr-form-file-queue-item" id="lr_filequeue_' + item.F_Id + '" ></div>');
            $item.append('<div class="lr-file-image"><img src="' + top.$.rootUrl + '/Content/images/filetype/' + item.F_FileType + '.png" alt="" onerror="javascript:this.src=\'/Content/images/filetype/Scheme.png\'" ></div>')
            $item.append('<span class="lr-file-name">' + item.F_FileName + '(' + learun.countFileSize(item.F_FileSize) + ')</span>');
            if (openForm == "View") {
            } else {
                $item.append('<div class="lr-tool-bar"><i class="fa fa-cloud-download" title="下载"  data-value="' + item.F_Id + '" ></i></div>');
            }
            $item.find('.lr-tool-bar img').on('click', function () {
                var fileId = $(this).attr('data-value');
                var fileType = $(this).attr('F_FileType');
                if (ConstantZSJT.PREVIEWPICTURE.indexOf(fileType) > -1) {
                    ImagePreview.init({ id: fileId, url: 'data-original' });
                }
            });
            $item.find('.lr-tool-bar .fa-cloud-download').on('click', function () {
                var fileId = $(this).attr('data-value');
                DownFile(fileId);
            });
            $item.find('.lr-tool-bar .fa-eye').on('click', function () {
                var fileId = $(this).attr('data-value');
                var fileType = $(this).attr('F_FileType');
                if (ConstantZSJT.PREVIEWPICTURE.indexOf(fileType) <= -1) {
                    openViewForm(fileId, fileType);
                }
            });
            $('#lr_form_file_queue_list').append($item);
            if (ConstantZSJT.PREVIEWPICTURE.indexOf(item.F_FileType) > -1) {
                ImagePreview.init({ id: item.F_Id, url: 'data-original' });
            }
        }
    });
    // 下载文件
    var DownFile = function (fileId) {
        learun.download({ url: top.$.rootUrl + '/LR_SystemModule/Annexes/DownAnnexesFile', param: { fileId: fileId, __RequestVerificationToken: $.lrToken }, method: 'POST' });
    }
    var openViewForm = function (fileId, fileType) {
        if (ConstantZSJT.PREVIEWFILEEXT.indexOf(fileType) > -1) {
            //通过接口获取
            learun.httpAsyncGet(top.$.rootUrl + '/LR_SystemModule/Annexes/GetPdf?fileId=' + fileId, function (data) {
                if (data.code == learun.httpCode.success) {
                    var pdfUrl = data.data.filepath;
                    if (data.data.AnnexesFileToIIS == "1") {
                        learun.layerForm({
                            id: 'PreviewForm',
                            title: '文件预览',
                            url: "/Content/openPdf/web/viewer.html?file=" + pdfUrl.substring(pdfUrl.indexOf("/")),
                            width: 1080,
                            height: 850,
                            maxmin: true,
                            btn: null
                        });
                    } else {
                        learun.layerForm({
                            id: 'PreviewForm',
                            title: '文件预览',
                            url: top.$.rootUrl + '/LR_SystemModule/Annexes/PreviewFile?fileId=' + fileId,
                            width: 1080,
                            height: 850,
                            maxmin: true,
                            btn: null
                        });
                    }
                } else {
                    learun.layerForm({
                        id: 'PreviewForm',
                        title: '文件预览',
                        url: top.$.rootUrl + '/LR_SystemModule/Annexes/PreviewFile?fileId=' + fileId,
                        width: 1080,
                        height: 850,
                        maxmin: true,
                        btn: null
                    });
                }
            });
        } else {
            learun.alert.warning(fileType + "类型文件不支持预览！");
            //learun.download({ url: top.$.rootUrl + '/LR_SystemModule/Annexes/DownAnnexesFile', param: { fileId: fileId, __RequestVerificationToken: $.lrToken }, method: 'POST' })
        }
    }
    $('#lr_form_file_queue').lrscroll();
}
